
package com.me.pikachu.main;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.OrthographicCamera;

public class MatrixTransformScreen {

    // start point, we have to calculate
    private float startX;

    private float startY;

    private float lastX;

    private float lastY;

    // element in matrix
    private int horizonElement;

    private int verticalElement;

    // the height and weight of each element
    private float heightElement;

    private float widthElement;

    // distance between two element
    private float heightDistance;

    private float widthDistance;

    // middle screen
    private float middleX;

    private float middleY;

    private OrthographicCamera camera;

    public MatrixTransformScreen(OrthographicCamera camera, int horizonElement, int verticalElement) {
        this.camera = camera;
        this.horizonElement = horizonElement;
        this.verticalElement = verticalElement;

        heightElement = 58;
        widthElement = 58;

        heightDistance = 8;
        widthDistance = 8;

        middleX = camera.viewportWidth / 2;
        middleY = camera.viewportHeight / 2;

        real2Game();
        determinStartPoint();
    }
    
    private void real2Game() {
        float scaleHorizon = Gdx.graphics.getWidth() / camera.viewportWidth;
        float scaleVertical = Gdx.graphics.getHeight() / camera.viewportHeight;

        // 1
        heightElement = scaleVertical * heightElement;
        widthElement = scaleHorizon * widthElement;

        // 2
        widthDistance = scaleHorizon * widthDistance;
        heightDistance = scaleVertical * heightDistance;

        // 3
        middleX = scaleHorizon * middleX;
        middleY = scaleVertical = middleY;
    }

    private void determinStartPoint() {
        startX = (2 * middleX - widthElement * horizonElement - (horizonElement - 1)
                * widthDistance) / 2;
        startY = (2 * middleY - heightElement * verticalElement - (verticalElement - 1)
                * heightDistance) / 2;

        lastX = 2 * middleX - startX;
        lastY = 2 * middleY - startY;
    }

    public Point Screen2Point(float screenX, float screenY) {
        Point p = null;

        if (startX <= screenX && lastX >= screenX) {
            if (startY <= screenY && lastY >= screenY) {
                screenY = Gdx.graphics.getHeight() - screenY;
                int i = (int) (Math.floor((screenX - startX) / (widthElement + widthDistance)) + 1);
                int j = (int) Math.floor((screenY - startY) / (heightElement + heightDistance)) + 1;
                p = new Point(horizonElement - j + 1, i - 1);
            }
        }

        return p;
    }

    public Point Point2Screen(int i, int j) {
        Point screen = null;
        i = horizonElement - i;
        // j = verticalElement - j;
        float screenX = startX + j * (heightElement + heightDistance);
        float screenY = startY + i * (widthElement + widthDistance);
        screen = new Point(screenX, screenY);
        return screen;
    }

    public float getHeightElement() {
        return heightElement;
    }

    public float getWeightElement() {
        return widthElement;
    }

    public int getHorizonElement() {
        return horizonElement;
    }

    public int getVerticalElement() {
        return verticalElement;
    }
}
